

foreach yr in 11 12 13 14 {

	foreach prd in POS PPO {
		
		use "$temp\Master file`yr'.dta", clear
		keep if prod == "`prd'"
		
		preserve
			drop if out_option == 1
			keep wtd_price calc_allwd los female age_band_cd los aha_hnpi drg_wt`yr'
			tab age_band_cd, gen(age)
			foreach a in 2 3 4 5 6 {
				gen female`a' = female*age`a'
			}
			
			areg wtd_price los female age2 age3 age4 age5 age6 female2 female3 female4 female5 female6, a(aha_hnpi)
			predict xbeta_hat,
			predict hosp_dummy, d
			egen mean_pop = mean(xbeta_hat)

			collapse (mean) avg_allwd=calc_allwd avg_drg_wt = drg_wt`yr', by(aha_hnpi mean_pop hosp_dummy) fast
			gen hosp_price = mean_pop + hosp_dummy

			keep aha_hnpi hosp_price avg_allwd
			sum hosp_price, det
			
			gen ltp1 = hosp_price < r(p1)
			gen gtp99 = hosp_price > r(p99)
			
			save "$temp\hosp_prices.dta", replace
		restore

		merge m:1 aha_hnpi using "$temp\hosp_prices.dta"
		drop _m newborn_flag serv dstatus
		
		save "$temp\Master file`yr'_`prd'.dta", replace
	}
	
	use "$temp\Master file`yr'_POS.dta", clear
	append using "$temp\Master file`yr'_PPO.dta"
	
	save "$output\Master file`yr'.dta", replace
}

exit
